
var CurrentUser;    //Global variable

function getName() {
    var user = JSON.parse(localStorage.getItem("loggedinUser"));
    CurrentUser = user;   //global variable
    if (user) {
        console.log("Currently logged in:  " + user["name"]);
        document.getElementById("name-goes-here").innerText = user["name"];
    } else {
        console.log("No one is logged in");
    }
}
getName(); //run the function

function populateHistory() {
    // find out who is logged in 
    var who = JSON.parse(localStorage.getItem("loggedinUser")); 
    var users = JSON.parse(localStorage.getItem("users")); 
    let email = who["email"]; 
    const user = users.find(u => u.email === email); 
    // get information for that user who is logged in
    if (user) {
        historyArray = user["history"];
        historyArray.forEach((hist) => {
            let historyCardTemplate = document.getElementById("postCardTemplate");
            let historyCardGroup = document.getElementById("history-goes-here");
            let onecard = historyCardTemplate.content.cloneNode(true);

            onecard.querySelector(".date").innerHTML = hist.date;
            onecard.querySelector(".count").innerHTML = hist.calories;
            onecard.querySelector(".card-description").innerHTML = hist.details;
            onecard.querySelector('.card-image').src =" ./images/"+ hist.imageName;
            historyCardGroup.appendChild(onecard);
            })
    } else {
    console.log("No one is logged in");
    }
    }
    populateHistory();
